//
//  ZXRomSettleCategoryManage.m
//  XingChangTong
//
//  Created by 戴明亮 on 16/9/27.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZXRomSettleCategoryManage.h"

@interface ZXRomSettleCategoryManage ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIView *viewLine;
//@property (nonatomic, strong) NSMutableArray *arrayTitle;
@property (nonatomic, strong) NSMutableArray *arrayButton;
@property (nonatomic, assign) int buttonIndex;

// 设置当前选中第几个
@property (nonatomic, assign)  int selectIndex;


@property (nonatomic, strong) UIScrollView *scrollView;


@end

@implementation ZXRomSettleCategoryManage


- (UIView *)viewLine
{
    if (_viewLine == nil) {
        _viewLine = [[UIView alloc] init];
        _viewLine.backgroundColor = [UIColor darkGrayColor];
        //RomColor(255, 68, 4);
        [self.scrollView addSubview:_viewLine];
    }
    return _viewLine;
}

- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.delegate = self;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 0);
        //_scrollView.backgroundColor = [UIColor brownColor];
       
    }
    return _scrollView;
}

- (NSMutableArray *)arrayButton
{
    if (_arrayButton == nil) {
        _arrayButton = [NSMutableArray arrayWithCapacity:0];
    }
    return _arrayButton;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame WithSelectIndex:(int)selectIndex
{
    self = [super initWithFrame:frame];
    if (self) {
        _selectIndex = selectIndex;
        _isScrollEnable = NO;
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.scrollView];
        self.scrollView.frame =  CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, frame.size.height);
        
    }
    
    return self;
}


- (void)setArrayTitle:(NSMutableArray *)arrayTitle
{
    _arrayTitle = arrayTitle;
    int count = _arrayTitle.count;
    CGFloat leRiInSet = 10;
    //  CGFloat padingRow = 10; // 行间距
    CGFloat padingCow = 10; // 列间距
    
    
    if (_isScrollEnable == YES) {
        
        
        CGFloat buttonH   = 40 - 1;
        
        
        [self.arrayButton removeAllObjects];
        for (int i = 0; i < count; i ++) {
            NSString *titleString = _arrayTitle[i];
            CGFloat buttonW  = [titleString boundingRectWithSize:CGSizeMake(MAXFLOAT, buttonH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil].size.width + 20;
            
            // RomColor(153, 153, 153)
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            UIButton *buttonLast  = [self.arrayButton lastObject];
            CGFloat X = leRiInSet;
            if ( i == 0) {
                X = leRiInSet;
            }else{
                X = CGRectGetMaxX(buttonLast.frame) + 10;
            }
  
            ///leRiInSet + (buttonW + padingCow)*i;
            CGFloat Y = 0;
            // tag 不能设置为0 否则找不到视图
            button.tag =1 + i;
            button.frame = CGRectMake(X,Y, buttonW, buttonH);
            [button setTitle:arrayTitle[i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(btnCliclk:) forControlEvents:UIControlEventTouchUpInside];
            [self.arrayButton addObject:button];
            [self.scrollView addSubview:button];
            
            //button.backgroundColor = [UIColor blueColor];
            NSLog(@"button位置======> %@",NSStringFromCGRect(button.frame));
            
            
            // 防止外界传入的值越界
            if (_selectIndex < 0 || _selectIndex > 4) {
                _selectIndex = 0;
            }
            
            if (i == _selectIndex) {
                // RomColor(255, 68, 4)
                [button setTitleColor:[UIColor colorWithRed:255/255.0 green:68/255.0 blue:4/255.0 alpha:1] forState:UIControlStateNormal];
                self.viewLine.frame = CGRectMake(X, buttonH, buttonW, 1);
                _buttonIndex = button.tag ;
            }
        }
        
        UIButton *senderLast = self.arrayButton.lastObject;
        
        self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(senderLast.frame), 0);
    }else{
        
        CGFloat buttonW   = ([UIScreen mainScreen].bounds.size.width - 2*leRiInSet - (count-1)*padingCow) / count;
        CGFloat buttonH   = 40 - 1;
        
        
        [self.arrayButton removeAllObjects];
        for (int i = 0; i < count; i ++) {
            
            NSString *titleString = _arrayTitle[i];
            CGFloat line_Width  = [titleString boundingRectWithSize:CGSizeMake(MAXFLOAT, buttonH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil].size.width + 20;
            
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            CGFloat X = leRiInSet + (buttonW + padingCow)*i;
            CGFloat Y = 0;
            // tag 不能设置为0 否则找不到视图
            button.tag =1 + i;
            button.frame = CGRectMake(X,Y, buttonW, buttonH);
            [button setTitle:arrayTitle[i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(btnCliclk:) forControlEvents:UIControlEventTouchUpInside];
            [self.arrayButton addObject:button];
            [self.scrollView addSubview:button];
            
            // 防止外界传入的值越界
            if (_selectIndex < 0 || _selectIndex > 4) {
                _selectIndex = 0;
            }
            
            if (i == _selectIndex) {
                [button setTitleColor:[UIColor colorWithRed:255/255.0 green:68/255.0 blue:4/255.0 alpha:1] forState:UIControlStateNormal];
                self.viewLine.center = CGPointMake(button.center.x, buttonH);
                self.viewLine.bounds = CGRectMake(0, 0, line_Width, 1);
                //self.viewLine.frame = CGRectMake(X, buttonH, buttonW, 1);
                _buttonIndex = button.tag ;
            }
            
        }
        self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 0);
        
    }
    
    
   
    
}



/// 按钮点击事件
- (void)btnCliclk:(UIButton *)btn
{
    NSLog(@"%s",__func__);
    UIButton *lastBtn = [self viewWithTag:_buttonIndex];
    
   
    CGFloat line_Width  = [btn.titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 39) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil].size.width + 20;

   
    
    [UIView animateWithDuration:0.2 animations:^{
        [lastBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        
        [btn setTitleColor:[UIColor colorWithRed:255/255.0 green:68/255.0 blue:4/255.0 alpha:1] forState:UIControlStateNormal];
        
        self.viewLine.center = CGPointMake(btn.center.x, 39);
        self.viewLine.bounds = CGRectMake(0, 0, line_Width, 1);
   
//        CGRect rect = self.viewLine.frame;
//        rect.origin.x = btn.frame.origin.x;
//        rect.size.width = btn.frame.size.width;
//        self.viewLine.frame = rect;
       
    } completion:^(BOOL finished) {
        
    }];

     _buttonIndex = btn.tag;
    
    // 调整位置
    if (self.isScrollEnable == YES) {
        
        CGFloat offsetX = btn.center.x - self.scrollView.bounds.size.width * 0.5;
        
        if (offsetX < 0) {
            offsetX = 0;
        }
        
        if (offsetX > (self.scrollView.contentSize.width - self.scrollView.bounds.size.width)) {
            offsetX = self.scrollView.contentSize.width - self.scrollView.bounds.size.width;
        }
        
        [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
        
    }
    
    if ([_delegate respondsToSelector:@selector(ZXRomSettleCategoryManage:WithButton:)]) {
        [_delegate ZXRomSettleCategoryManage:self WithButton:btn];
    }
    
    
}

- (void)setCurrentPage:(int)currentPage
{
    _currentPage = currentPage;
    // 当前选择按钮
    UIButton *currentBtn = [self viewWithTag:currentPage];
   // 上一次选择按钮
    UIButton *lastBtn = [self viewWithTag:_buttonIndex];
    
    
     CGFloat line_Width  = [currentBtn.titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 39) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil].size.width + 20;
    
    [UIView animateWithDuration:0.2 animations:^{
        [lastBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        
        [currentBtn setTitleColor:[UIColor colorWithRed:255/255.0 green:68/255.0 blue:4/255.0 alpha:1] forState:UIControlStateNormal];
//        CGRect rect = self.viewLine.frame;
//        rect.origin.x = currentBtn.frame.origin.x;
//        self.viewLine.frame = rect;
        self.viewLine.center = CGPointMake(currentBtn.center.x, 39);
        self.viewLine.bounds = CGRectMake(0, 0, line_Width, 1);
        
        
    } completion:^(BOOL finished) {
        
    }];
   _buttonIndex = currentBtn.tag;

}


- (void)adjustButtonFrame:(int)targetIndex
{
   
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
